package edu.fsu.cs.SHPE;
import java.net.ConnectException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;


public class ConferenceDisplayActivity extends Activity {

	private final static String TAG = "SHPE.ConferenceDisplayActivity";
	private long conferenceId;

	private VenueListAdapter venueAdapter;
	private SpeakerListAdapter speakersAdapter;
	private SessionListAdapter sessionAdapter;
	private SponsorListAdapter sponsorAdapter;
	
	private ListView listView;
	private OnItemClickListener venueItemClickListener, speakersItemClickListener, sessionsItemClickListener, sponsorsItemClickListener;
	private Context context;
	private Spinner tracks, sponsorLevels;
	private ArrayAdapter<String> tracksAdapter, sponsorLevelAdapter;
	private OnItemSelectedListener tracksItemSelectedListener, sponsorLevelItemSelectedListener;
	private TracksContainer tracksContainer;
	private SponsorLevelContainer sponsorLevelContainer;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		setContentView(R.layout.conferencedisplay);
		setTitle("Conference Information");

		conferenceId = getIntent().getLongExtra("id", -1);

		if(conferenceId == -1){
			Toast.makeText(this, "Error. No such conference id (" + conferenceId + ")", Toast.LENGTH_LONG).show();
			finish();
		}


		tracks = (Spinner)findViewById(R.id.spnTracks);
		sponsorLevels = (Spinner)findViewById(R.id.spnSponsorLevels);

		context = this;


		setOnClickListeners();
		setButtonIcons();
		displayDefault();
	}


	private void displayDefault() {
		listView.setAdapter(sessionAdapter);
		tracks.setAdapter(tracksAdapter);
		tracks.setVisibility(View.VISIBLE);
		tracks.setOnItemSelectedListener(tracksItemSelectedListener);
		listView.setOnItemClickListener(sessionsItemClickListener);
		
		sponsorLevels.setAdapter(sponsorLevelAdapter);
		sponsorLevels.setOnItemSelectedListener(sponsorLevelItemSelectedListener);
		
	}


	private void setOnClickListeners() {
		try {
			tracksContainer = new TracksContainer(Conference.insertId(getString(R.string.getTracks), conferenceId));
			sponsorLevelContainer = new SponsorLevelContainer(Conference.insertId(getString(R.string.getSponsorsLevels), conferenceId));
			
			for(String s : tracksContainer.getStrings())
				Log.i("Track String", s);


			tracksAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, tracksContainer.getStrings());
			tracksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			tracksItemSelectedListener = new TracksItemSelectedListener();

			sponsorLevelAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, sponsorLevelContainer.getStrings());
			sponsorLevelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sponsorLevelItemSelectedListener = new SponsorLevelItemSelectedListener();
			
			

			listView = (ListView) findViewById(R.id.listView);
			venueAdapter = new VenueListAdapter(this,Conference.insertId(getString(R.string.getVenus), conferenceId));
			venueItemClickListener = new VenueItemClickListener();

			sessionAdapter = new SessionListAdapter(this, Conference.insertId(getString(R.string.getSessions), conferenceId));
			sessionsItemClickListener = new SessionsItemClickListener();


			speakersAdapter = new SpeakerListAdapter(this, Conference.insertId(getString(R.string.getSpeakers), conferenceId));
			speakersItemClickListener = new SpeakersItemClickListener();
			
			
			sponsorAdapter = new SponsorListAdapter(this, Conference.insertId(getString(R.string.getSponsors), conferenceId));
			sponsorsItemClickListener = new SponsorsItemClickListener();
			
			
		} catch (ConnectException e) {
			Toast.makeText(this, "Unable to Connect", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}

	}


	private void setButtonIcons() {

		Display display = getWindowManager().getDefaultDisplay();
		int pixels = 0;

		if(display.getOrientation() == 0)
			pixels = display.getWidth()/6;
		else
			pixels = display.getHeight()/6;

		Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.venues);
		ImageButton imgButton = (ImageButton) findViewById(R.id.btnVenues);
		imgButton.setImageBitmap(Bitmap.createScaledBitmap(image, pixels, pixels, false));
		imgButton.setMinimumWidth(display.getWidth()/4);

		image = BitmapFactory.decodeResource(getResources(), R.drawable.calendar);
		imgButton = (ImageButton) findViewById(R.id.btnSessions);
		imgButton.setImageBitmap(Bitmap.createScaledBitmap(image, pixels, pixels, false));
		imgButton.setMinimumWidth(display.getWidth()/4);

		image = BitmapFactory.decodeResource(getResources(), R.drawable.mic);
		imgButton = (ImageButton) findViewById(R.id.btnSpeakers);
		imgButton.setImageBitmap(Bitmap.createScaledBitmap(image, pixels, pixels, false));
		imgButton.setMinimumWidth(display.getWidth()/4);

		image = BitmapFactory.decodeResource(getResources(), R.drawable.money);
		imgButton = (ImageButton) findViewById(R.id.btnSponsors);
		imgButton.setImageBitmap(Bitmap.createScaledBitmap(image, pixels, pixels, false));
		imgButton.setMinimumWidth(display.getWidth()/4);
	}


	public void onClick(View view){

		Log.i("onclick", "view id " + view.getId());

		if(view.getId() == R.id.btnSessions){
			listView.setAdapter(sessionAdapter);
			tracks.setVisibility(View.VISIBLE); 
			listView.setOnItemClickListener(sessionsItemClickListener);
		}
		else
			tracks.setVisibility(View.GONE);


		if(view.getId() == R.id.btnVenues){
			listView.setAdapter(venueAdapter);
			listView.setOnItemClickListener(venueItemClickListener);
		}

		if(view.getId() == R.id.btnSpeakers){
			listView.setAdapter(speakersAdapter);
			listView.setOnItemClickListener(speakersItemClickListener);
		}
		
		if(view.getId() == R.id.btnSponsors){
			listView.setAdapter(sponsorAdapter);
			listView.setOnItemClickListener(sponsorsItemClickListener);
			sponsorLevels.setVisibility(View.VISIBLE);
		}
		else
			sponsorLevels.setVisibility(View.GONE);

	}

	private class TracksItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Log.i("track selected", "position " + position + ", id " + id);

			sessionAdapter.setViewableSessionsByTrackId(tracksContainer.get(position).getId());
			sessionAdapter.notifyDataSetChanged();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {


		}
	}
	
	private class SponsorLevelItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Log.i("track selected", "position " + position + ", id " + id);

			sponsorAdapter.setViewableSponsorsByLevelId(sponsorLevelContainer.get(position).getId());
			sponsorAdapter.notifyDataSetChanged();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	

	private class SessionsItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.i(TAG, "Session Click position " + position + " id "+ id);


			//Session.addToCalendar(context, "test", 1, 1);

			Intent intent = new Intent(context,SessionActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("id", sessionAdapter.getItemId(position));

			context.startActivity(intent);

		}

	}


	private class SpeakersItemClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			final Speaker tmpSpeaker = (Speaker) speakersAdapter.getItem(position);

			final CharSequence[] options = {"Email", "Website", "LinkedIn", "Read Bio"};

			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle("What do you want to do?");
			dialog.setItems(options, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();

					switch(which){
					case 0:
						if(tmpSpeaker.getEmail() != null){
							intent.putExtra(Intent.EXTRA_EMAIL, new String[]{tmpSpeaker.getEmail()});
							intent.setAction(Intent.ACTION_EDIT);
							intent.setType("plain/text");
						}
						break;

					case 1:
						if(tmpSpeaker.getWebsite() != null){
							intent.setData(Uri.parse(tmpSpeaker.getWebsite()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 2:
						if(tmpSpeaker.getLinkedIn() != null){
							intent.setData(Uri.parse(tmpSpeaker.getLinkedIn()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 3:
						intent.setClass(context, SpeakerActivity.class);
						intent.setAction(Intent.ACTION_VIEW);
						intent.putExtra("id", tmpSpeaker.getId());
						break;
					}

					if(intent.getAction() != null)
						context.startActivity(intent);

				}
			});

			AlertDialog alert;

			if(tmpSpeaker != null){
				alert = dialog.create();
				alert.show();
			}


		}

	}

	private class SponsorsItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
			Intent intent = new Intent(context,SponsorActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("id", sponsorAdapter.getItemId(position));
			context.startActivity(intent);
			
		}
		
	};
	
	
	private class VenueItemClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.i("venueclicklistener", "position " + position + ", id" + id);


			final Venue tmpVenue = (Venue) venueAdapter.getItem(position);


			final CharSequence[] options = {"Call", "Map", "Website"};

			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle("What do you want to do?");
			dialog.setItems(options, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();

					switch(which){
					case 0:
						if(tmpVenue.getPhone() != null){
							intent.setData(Uri.parse("tel:" + tmpVenue.getPhone()));
							intent.setAction(Intent.ACTION_DIAL);
						}
						break;
					case 1:
						if(tmpVenue.getLat() != null && tmpVenue.getLon() != null){ 
							intent = new Intent(context, VenueMapActivity.class);
							intent.putExtra("venueId", tmpVenue.getId());
							//intent.putExtra("lat", Double.parseDouble(tmpVenue.getLat()));
							//intent.putExtra("lon", Double.parseDouble(tmpVenue.getLon()));
							intent.putExtra("zoom", Integer.parseInt(context.getString(R.string.zoomclose)));
							intent.putExtra("conferenceId", conferenceId);
							//intent.setData(Uri.parse("geo:" + tmpConference.getLat() + "," + tmpConference.getLon()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					case 2:
						if(tmpVenue.getWebsite() != null){
							intent.setData(Uri.parse(tmpVenue.getWebsite()));
							intent.setAction(Intent.ACTION_VIEW);
						}
						break;
					default:
						intent = null;
						break;
					}

					if(intent != null){
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}
					else
						Toast.makeText(context, "Option Not Available", Toast.LENGTH_SHORT).show();

				}
			});

			AlertDialog alert;

			if(tmpVenue != null){
				alert = dialog.create();
				alert.show();
			}
		}
	}
}
